﻿using MailKit.Net.Smtp;
using MimeKit;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;

namespace DGZImp.WebApi.Services;  // 命名空间与目录一致

public class EmailService
{
    private readonly IConfiguration _config;

    public EmailService(IConfiguration config)
    {
        _config = config;
    }

    public async Task SendEmailAsync(string toEmail, string subject, string body)
    {
        var email = new MimeMessage();

        email.From.Add(new MailboxAddress(
            "CCD报工",
            "liuyujie@atmcn.com"
        ));
        email.To.Add(MailboxAddress.Parse(toEmail));
        email.Subject = subject;
        email.Body = new TextPart("plain") { Text = body };

        using var smtp = new SmtpClient();
        await smtp.ConnectAsync(
            "smtp.exmail.qq.com",
            465,
            MailKit.Security.SecureSocketOptions.SslOnConnect
        );
        await smtp.AuthenticateAsync(
            "liuyujie@atmcn.com",
            "Liu@12345"
        );
        await smtp.SendAsync(email);
        await smtp.DisconnectAsync(true);
    }
}
